#include <common.h>
#include <dm.h>
#include <errno.h>
#include <fdtdec.h>
#include <malloc.h>
#include <dm-demo.h>
#include <asm/io.h>
#include <asm/gpio.h>
#include <mapmem.h>

DECLARE_GLOBAL_DATA_PTR;

struct imx6ull_data {
	int num_chars;	/* Number of non-space characters output so far */
	struct gpio_desc gpio_desc[8];
	int gpio_count;
};

static int imx6ull_hello(struct udevice *dev, int ch)
{
	const struct dm_demo_pdata *pdata = dev_get_platdata(dev);
	struct imx6ull_data *data = dev_get_priv(dev);

	printf("Hello from %08x: %s %d\n", (uint)map_to_sysmem(dev), pdata->colour,
	       pdata->sides);

	data->num_chars = ch;

	return 0;
}

static int imx6ull_status(struct udevice *dev, int *status)
{
	struct imx6ull_data *data = dev_get_priv(dev);

	*status = data->num_chars;
	return 0;
}

static int imx6ull_set_light(struct udevice *dev, int light)
{
	struct imx6ull_data *priv = dev_get_priv(dev);
	struct gpio_desc *desc;
	int ret;
	int i;

	desc = priv->gpio_desc;
	for (i = 0; i < priv->gpio_count; i++, desc++) {
		uint mask = 1 << i;

		ret = dm_gpio_set_value(desc, light & mask);
		if (ret < 0)
			return ret;
	}

	return 0;
}

static int imx6ull_get_light(struct udevice *dev)
{
	struct imx6ull_data *priv = dev_get_priv(dev);
	struct gpio_desc *desc;
	uint value = 0;
	int ret;
	int i;

	desc = priv->gpio_desc;
	for (i = 0; i < priv->gpio_count; i++, desc++) {
		uint mask = 1 << i;

		ret = dm_gpio_get_value(desc);
		if (ret < 0)
			return ret;
		if (ret)
			value |= mask;
	}

	return value;
}


static const struct demo_ops imx6ull_ops = {
	.hello = imx6ull_hello,
	.status = imx6ull_status,
	.get_light = imx6ull_get_light,
	.set_light = imx6ull_set_light,
};

static int imx6ull_ofdata_to_platdata(struct udevice *dev)
{
	struct dm_demo_pdata *pdata = dev_get_platdata(dev);
	int ret;

	/* Parse the data that is common with all demo devices */
	ret = demo_parse_dt(dev);
	if (ret)
		return ret;

	/* Parse the data that only we need */
	pdata->default_char = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
					     "character", 'T');

	return 0;
}


static const struct udevice_id demo_imx6ull_id[] = {
	{ .compatible = "fsl,imx6ull-demo" },
	{ }
};

static int dm_imx6ull_probe(struct udevice *dev)
{
	struct imx6ull_data *priv = dev_get_priv(dev);
	int ret;

	ret = gpio_request_list_by_name(dev, "led-gpios", priv->gpio_desc,
					ARRAY_SIZE(priv->gpio_desc),
					GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
	if (ret < 0)
		return ret;
	priv->gpio_count = ret;
	//printf("%s: %d GPIOs\n", __func__, priv->gpio_count);

	return 0;
}

static int dm_imx6ull_remove(struct udevice *dev)
{
	struct imx6ull_data *priv = dev_get_priv(dev);

	return gpio_free_list(dev, priv->gpio_desc, priv->gpio_count);
}


U_BOOT_DRIVER(demo_imx6ull_drv) = {
	.name	= "demo_imx6ull_drv",
	.of_match = demo_imx6ull_id,
	.id	= UCLASS_DEMO,
	.ofdata_to_platdata = imx6ull_ofdata_to_platdata,
	.ops	= &imx6ull_ops,
	.probe = dm_imx6ull_probe,
	.remove = dm_imx6ull_remove,
	.priv_auto_alloc_size = sizeof(struct imx6ull_data),
	.platdata_auto_alloc_size = sizeof(struct dm_demo_pdata),
};

